package com.probox.apiModules.controller.customer;


import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.service.customer.ICustomerCollectService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.VerifyAuthorize;
import com.probox.customer.api.domain.CustomerCollect;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
 * 开放给外部访问的客户服务
 */
@Api(value = "客户模块", tags = {"客户收藏前端操作接口"})
@RestController
@RequestMapping("/customer/openCustomercollect")
public class OpenCustomerCollectController extends BaseController {

    @Autowired
    private ICustomerCollectService customerCollectService;

    /**
     * 查询客户收藏记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerCollect customerCollect) {
        startPage();
        List<CustomerCollect> list = customerCollectService.queryList(customerCollect);
        return getDataTable(list);
    }

    /**
     * 导出客户收藏记录列表
     */

    @Log(title = "客户收藏记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerCollect customerCollect) throws IOException {
        List<CustomerCollect> list = customerCollectService.queryList(customerCollect);
        ExcelUtil<CustomerCollect> util = new ExcelUtil<CustomerCollect>(CustomerCollect.class);
        util.exportExcel(response, list, "客户收藏记录数据");
    }

    /**
     * 获取客户收藏记录详细信息
     */
    @GetMapping(value = "/{collectUid}")
    public AjaxResult getInfo(@PathVariable("collectUid") Long collectUid) {
        return AjaxResult.success(customerCollectService.selectCustomerCollectById(collectUid));
    }

    /**
     * 新增客户收藏记录
     */
    @ApiOperation(value = "新增客户收藏记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK")
    })
    @PostMapping
    @VerifyAuthorize
    public AjaxResult add(@RequestBody CustomerCollect customerCollect) {
        /*
        customerCollect.setCollectTime(new Date());
        customerCollect.setCollectStatus(DataStatus.valid.getKey());
        customerCollect.setCustomerName(SecurityUtils.getCustomerUserName());
        customerCollect.setCustomerUid(SecurityUtils.getCustomerUid());

         */
        return toAjax(customerCollectService.insertCustomerCollect(customerCollect));
    }

    /**
     * 修改客户收藏记录
     */
    @ApiOperation(value = "修改客户收藏记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK")
    })
    @PutMapping
    @VerifyAuthorize
    public AjaxResult edit(@RequestBody CustomerCollect customerCollect) {



        return toAjax(customerCollectService.updateCustomerCollect(customerCollect));
    }

    /**
     * 删除客户收藏记录
     */
    @ApiOperation(value = "客户批量收藏记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK")
    })
    @PostMapping("/batchAdd/{lunwenUids}")
    @VerifyAuthorize
    public AjaxResult batchAdd(@PathVariable Long[] lunwenUids) {
        CustomerCollect collect = new CustomerCollect();
        for (Long id:lunwenUids) {
            collect.setObjUid(id);
            collect.setObjType("good");
            customerCollectService.batchCollect(collect);
        }
        return toAjax(true);
    }

    /**
     * 当前用户收藏和取消收藏商品
     * @ objType 收藏类型
     * @ objUid 对象id
     */
    @ApiOperation(value="当前用户收藏和取消收藏商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "objType", value = "收藏类型(store商铺/good商品)", paramType = "String", required = true),
            @ApiImplicitParam(name = "objUid", value = "对象id", paramType = "Long", required = true)
    })
    @PostMapping("/editCollectStatus")
    @VerifyAuthorize
    public AjaxResult editCollectStatus(@RequestBody CustomerCollect customerCollect){
        String collectStatus = customerCollectService.editCollectStatus(customerCollect);
        return AjaxResult.success("操作成功",collectStatus);
    }

    /**
     * 查询当前客户所有收藏信息
     */
    @ApiOperation(value="查询当前客户所有收藏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "总条数", paramType = "Integer", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = CustomerCollect.class),
    })
    @GetMapping("/getCustomerAllCollect")
    @VerifyAuthorize
    public AjaxResult getCustomerAllCollect(CustomerCollect customerCollect){
        startPage();
        List<CustomerCollect> customerCollectList =  customerCollectService.getCustomerAllCollect(customerCollect);
        Integer customerCollectListCount =  customerCollectService.getCustomerAllCollectCount(customerCollect);
        return AjaxResult.successs(customerCollectList,customerCollectListCount);
    }

}
